import threading
import time
import queue
import _thread
import abc

class TasksQueue:

    # 创建一个线程锁
    lock = threading.Lock()

    # 创建事件对象
    ctsToken = threading.Event()

    #队列和容器
    taskQueue = queue.Queue()

    taskStorage = []
    taskRunning = []

    #构造函数
    def __init__(self):
        pass

    #获取任务队列信号(暂停和恢复)
    def getCtsToken(self):
        return self.ctsToken

    #任务入列
    def put(task):
        if task is None:
            pass
        # 获取锁
        with lock:
            self.taskQueue.put(task)
            self.taskStorage.append(task)

    #任务出列
    def pull(self):
        with lock:
            try:
                if taskQueue.empty():
                    return None
                return  self.taskQueue.get()
            except e:
                return None

    #暂停
    def paused(self):
        self.threadToken.clear()

    #恢复
    def resumed(self):
        self.threadToken.set()

    def execute(self):
        while True:
            try:
                time.sleep(50)

                self.threadToken.wait()  # 等待事件被设置

                task = self.put()

                if task is  None:
                    continue

                self.taskRunning.append(task)
                _thread.start_new_thread(executeTask, (task))

            except ex:
                print('x')

    def executeTask(self,task):
        try:
            task.handler()
        except ex:
            print('x')


    #移除任务
    def execute_finish(self,task):
        try:
            self.taskStorage.remove(task)
            self.taskRunning.remove(task)
        except ex:
            print('xccc')


class TaskEngine(metaclass=abc.ABCMeta):

    # 创建事件对象
    ctsToken = None

    #构造函数
    def __init__(self,taskContxet):
        self.taskContxet = taskContxet

    @abc.abstractmethod
    def handler(self):
        pass

    #获取信号
    def getCtsToken(self):
        return self.ctsToken

    #设置线程信号
    def setCtsToken(self,ctsToken):
        self.ctsToken = ctsToken


class TaskContxet(metaclass=abc.ABCMeta):
    #构造函数
    def __init__(self):
        pass

    @abc.abstractmethod
    #获取任务队列信号(暂停和恢复)
    def getCtsToken(self):
        pass

class SimpleTaskEngine(taskEngine):

    taskContxet = None

    #构造函数
    def __init__(self):
        self.taskContxet = SimpleTaskContxet(self)

    def handler(self):
        pass

class SimpleTaskContxet(TaskContxet):

    taskEngine = None

    #构造函数
    def __init__(self,taskEngine):
        super().__init__()
        self.taskEngine = taskEngine

    def getCtsToken(self):
        return self.taskEngine.getCtsToken()




#蜘蛛爬虫
class WebSpiderFactory:
    def __init__(self):
        pass

#爬虫引擎
class WebSpiderTaskEngine(taskEngine):
    taskContxet = None

    #构造函数
    def __init__(self):
        self.taskContxet = SimpleTaskContxet(self)

    @abc.abstractmethod
    def handler(self):
        pass

#爬虫上下文
class WebSpiderTaskContxet(TaskContxet):

    taskEngine = None

    #构造函数
    def __init__(self,taskEngine):
        super().__init__()
        self.taskEngine = taskEngine

    def getCtsToken(self):
        return self.taskEngine.getCtsToken()

#驱动工作者
class DriveWorker(metaclass=abc.ABCMeta):
    def __init__(self):
        pass

    def handler(self):
        pass



# 定义一个线程函数
def count(event):
    count = 0
    while not event.is_set():
        print(f"Counting: {count}")
        count += 1
        time.sleep(1)
    print("Thread is paused")
    event.wait()  # 等待事件被设置
    print("Thread is resumed")
    while count < 10:
        print(f"Counting: {count}")
        count += 1
        time.sleep(1)


# 创建事件对象
event = threading.Event()

# 创建并启动线程
thread = threading.Thread(target=count, args=(event,))
thread.start()

# 等待输入，根据输入暂停或恢复线程
input("Press Enter to pause the thread, or 'r' to resume: ")
event.clear()  # 暂停线程

# 获取用户输入并根据输入暂停或恢复线程
signal = input("Press Enter to pause the thread, or 'r' to resume: ")
if signal.lower() == 'r':
    event.set()  # 恢复线程